<?php

class Jd_task_jd_list_model extends MY_Model
{


    public function __construct()
    {
        $this->modelename = 'Jd_task_jd_list_model';
        $this->tablename = 'task_jd_list';
        parent::__construct();
    }


    public function checktsak($taskid)
    {
        $sql = "select 1 from jd_task_jd_list  where   taskid=$taskid order by id asc limit 1 ";
        return $this->GetBySql($sql, 0);
    }

    /**
     *
     * 商户任务添加添加具体单号
     * @param array $array
     * @return INT
     */
    public function addtasklist($array = array(), $user_id = '', $picrow = array(), $textrow = array())
    {
        if (!$user_id) {
            $array['business_id'] = $this->userId;
        } else {
            $array['business_id'] = $user_id;
        }
        $array['addtime'] = time();
        $array['status'] = 0;
        $list = $array['list'];
        unset($array['list']);
        for ($x = 1; $x <= $list; $x++) {
            $array['list'] = $x;
            $array['showpicid'] = isset($picrow[$x - 1]) ? $picrow[$x - 1] : 0;
            $array['comment'] = isset($textrow[$x - 1]) ? $textrow[$x - 1] : 0;


            $r = $this->add($this->tablename, $array);
        }
        return $r;
    }


    /**
     * 获取排序最小的子单任务
     */
    public function getminlitstake($taskid, $select = "*")
    {
        $sql = "select $select from jd_task_jd_list  where  `status` =0  AND taskid=$taskid order by id asc limit 1 ";
        return $this->GetBySql($sql, 0);
    }


    /**
     * 通过任务ID 获取具体子单号
     * @param array $idstr
     */
    public function getlistidbytask($idstr = array())
    {
        $sql = 'select * from jd_task_jd_list  where  `status` =0 AND  taskid in (' . implode($idstr, ",") . ') ';
        return $result = $this->GetBySql($sql);
    }


    /**
     *
     * 多个任务被推送后更新状态
     * @param array $idstr
     */
    public function updateaccept($idstr = array())
    {
        $sql = 'update   jd_task_jd_list   set  `status` =1   where  `status` =0 AND  taskid in (' . implode($idstr, ",") . ') ';
        return $this->db->query($sql);
    }


    /**
     * 单个任务被推送后更新状态
     * @param $id
     * @return mixed
     */
    public function updateacceptone($id)
    {
        $sql = 'update   jd_task_jd_list   set  `status` =1   where  `status` =0 AND  id= ' . $id;
        return $this->db->query($sql);
    }

    /**
     *
     * 子单任务加入任务池后更新状态
     * @param array $idstr
     */
    public function updatapool($idstr = array())
    {
        if (is_array($idstr)) {
            $sql = 'update   jd_task_jd_list   set  `status` =1   where  `status` =0 AND  id in (' . implode($idstr, ",") . ') ';
        } else {
            $sql = 'update   jd_task_jd_list   set  `status` =1   where  `status` =0 AND  id= ' . $idstr;
        }
        return $this->db->query($sql);
    }


    /**
     * 更新任务实际用掉的钱
     */
    public function updaterealmoney($id, $row)
    {
        $real_price = $row['real_price'];
        $orderid = $row['orderid'];
        $awz = $row['awz'];
        $time = time();
        $sql = "update   jd_task_jd_list   set real_price=$real_price, orderid='$orderid', awz='$awz', `update`=$time   where  id=$id";
        // echo $sql; exit;
        $sql2 = "update   jd_task_jd_list_pool   set real_price=$real_price, orderid='$orderid', awz='$awz'  where  listid=$id";
        $this->db->query($sql2);
        return $this->db->query($sql);
    }


    /**
     * 计算所有
     */
    public function totalbunissmoney($star)
    {
        $now = time() + 600;
        $sql = "select sum(a.price*a.`number`-a.real_price) as 'balance',a.business_id as 'user_id', b.money,b.gold,b.recommender as 'saler',b.username,b.webid
from jd_task_jd_list  as a
left join jd_users as b on a.business_id=b.id 
left join jd_task_jd as c on a.taskid=c.id
 where a.addtime> $star AND (c.releasetime<$now  OR c.releasetime is null  OR c.releasetime =0 ) group by a.business_id";


        $result = $this->GetBySql($sql);

        $sql = "select sum(a.gold) as 'gold',a.business_id as 'user_id'
from jd_task_jd_list  as a
left join jd_users as b on a.business_id=b.id 
left join jd_task_jd as c on a.taskid=c.id
 where a.addtime> $star  AND a.status!=2  AND a.`status`<5 AND (c.releasetime<$now  OR c.releasetime is null  OR c.releasetime =0 ) group by a.business_id";
        $result2 = $this->GetBySql($sql);//获取没有完成任务的金币总数

        $goldlist = array();
        foreach ($result2 as $id => $v) {
            $goldlist[$v['user_id']] = $v['gold'];
        }
        foreach ($result as $id => $v) {
            $result[$id]['balancegold'] = isset($goldlist[$v['user_id']]) ? $goldlist[$v['user_id']] : 0;
        }
        return $result;
    }


    /**
     * 获取这个月的数据
     */
    public function getmonthdata($user_id)
    {
        $month = strtotime(date('Y-m-1'));
        $sql = "select  count(1) as 'all'
from jd_task_jd_list  
 where hunter_id=$user_id AND addtime>=$month AND  `status`=2";
        return $result = $this->GetBySql($sql, 0);
    }

    /**
     * 获取这个月的数据
     */
    public function getmonthdataall($type = 0)
    {
        $month = strtotime(date('Y-m-1'));

        $sql = "select  count(1) as 'all',`status`
from jd_task_jd_list  
 where addtime>=$month  AND `status`<3  group  by `status` ";

        if ($type) {
            $sql = "select  count(1) as 'all',`status`
from jd_task_jd_list  
 where addtime>=$month   AND `status`<3  AND `type`=$type group  by `status`";
        }
        return $result = $this->GetBySql($sql);
    }


    public function getweekdata($date1 = "", $date2 = "", $row = array())
    {

        $date1 = $date1 ? strtotime($date1) : strtotime(date('Y-m-d 00:00:00'));
        $date2 = $date2 ? strtotime($date2) : strtotime(date('Y-m-d 23:59:59'));
        $where = "addtime>=$date1  AND addtime<=$date2  AND `status`<3 ";
        if (isset($row['webid'])) {
            $webid = $row['webid'];
            $webid = intval($webid);
            if ($webid != 0) {
                $where .= " AND webid= $webid";
            }
        } else {
            $this->web_id = intval($this->web_id);
            $where .= " AND webid= $this->web_id";
        }
        $sql = "select  count(1) as 'all',`status`,sum(if((`type_h`=1 or `type_h`=3), 2, 3 ) ) as 'cost' ,sum(if((`type_h`=1 or `type_h`=3), 1.5, 2 ) ) as 'cost2',sum(if(`daifa`=1 , 1, 0 ) ) as 'cost3',sum(gold) as  'gold'
from jd_task_jd_list  
 where  " . $where . " group  by `status` order by `status` asc ";

        $sql = "select  count(1) as 'all',`status`,
sum( CASE `type_h` WHEN 1 THEN 2 WHEN 3 THEN 5.6 ELSE 3 END ) AS 'cost',
	sum( CASE `type_h` WHEN 1 THEN 1.5 WHEN 3 THEN 5 ELSE 2 END ) AS 'cost2',
 sum(if(`daifa`=1 , 0.99, 0 ) ) as 'cost3',
 sum(if(`dianzan`=1 ,0.25, 0 ) ) as 'cost4',
sum(if(`night`=1 ,1, 0 ) ) as 'cost5',
 
 sum(gold) as  'gold',
 sum(real_price) as 'real_price'
from jd_task_jd_list  
 where  " . $where . " group  by `status` order by `status` asc ";


        return $result = $this->GetBySql($sql);
    }


    public function getcost($date1 = "", $date2 = "", $row = array())
    {
        $date1 = $date1 ? strtotime($date1) : "";
        $date2 = $date2 ? strtotime($date2) : "";


        $where = "  p1.`status`=2  AND p1.webid=$this->web_id ";
        if (isset($row['webid'])) {
            $webid = $row['webid'];
            $where = "  p1.`status`=2  AND p1.webid=$webid ";
        }

        if ($date1) {
            $where .= " AND  p1.addtime>=$date1";
        }
        if ($date2) {
            $where .= " AND  p1.addtime<=$date2";
        }
        $sql = "select  count(1) as 'all',
sum( CASE `type_h` WHEN 1 THEN 2 WHEN 3 THEN 5.6 ELSE 3 END ) AS 'cost',
	sum( CASE `type_h` WHEN 1 THEN 1.5 WHEN 3 THEN 5 ELSE 2 END ) AS 'cost2',
sum(if(`daifa`=1 ,0.99, 0 ) ) as 'cost3',
sum(if(`dianzan`=1 ,0.25, 0 ) ) as 'cost4',
sum(if(`night`=1 ,1, 0 ) ) as 'cost5',
sum(p1.gold) as  'gold'
from jd_task_jd_list   as p1
left  join jd_users as p2 on p1.business_id=p2.id
 where " . $where;
        if (isset($row['salaid'])) {
            $salaid = $row['salaid'];
            $sql .= " AND p2.recommender_id=$salaid";
        }

        return $result = $this->GetBySql($sql, 0);
    }

    public function getSendNumber($date1 = "", $date2 = "", $row = array())
    {
        $date1 = $date1 ? strtotime($date1) : strtotime(date('Y-m-d 00:00:00'));
        $date2 = $date2 ? strtotime($date2) : strtotime(date('Y-m-d 23:59:59'));
        $where = "addtime>=$date1  AND addtime<=$date2  AND `status`<3 ";
        if (isset($row['webid'])) {
            $webid = $row['webid'];
            $webid = intval($webid);
            if ($webid != 0) {
                $where .= " AND webid= $webid";
            }
        } else {
            $this->web_id = intval($this->web_id);
            $where .= " AND webid= $this->web_id";
        }
        $sql = "select  count(DISTINCT(business_id) ) as 'sendnum'
from jd_task_jd_list  
 where  " . $where;
        return $result = $this->GetBySql($sql, 0);

    }


    /**
     *
     */
    public function getprofit()
    {


    }

    /**
     * 获取今天的数据
     * @param int $type
     * @return array
     */
    public function gettodayall($type = 0)
    {
        $month = strtotime(date('Y-m-d'));
        $sql = "select  count(1) as 'all',`status`
from jd_task_jd_list  
 where addtime>=$month   AND `status`<3  group  by `status`";
        if ($type) {
            $sql = "select  count(1) as 'all',`status`
from jd_task_jd_list  
 where addtime>=$month   AND `status`<3  AND `type`=$type group  by `status`";
        }
        return $result = $this->GetBySql($sql);
    }


    public function getdatebydate($data1, $data2, $web_id = '')
    {
        if (!$web_id) {
            $web_id = $this->web_id;
        }
        $data1 = strtotime($data1);
        $data2 = strtotime($data2);
        $sql = "select  count(1) as 'all' 
from jd_task_jd_list   as p1 
left join  jd_users as p2 on p1.business_id=p2.id   
 where  p1.webid =$web_id   AND p1.status<3";
        $sql .= " AND addtime>$data1 AND  addtime<$data2 AND   p2.iskefu=0 AND   p2.password!='qwer1234'";
        return $result = $this->GetBySql($sql, 0);
    }


    public function taskupdate($row, $primary = 'taskid')
    {
        $table = $this->tablename;
        $id = $row['id'];
        unset($row['id']);
        return $this->update($table, $primary, $id, $row, 100000); // TODO: Change the autogenerated stub
    }


    /**
     * 废弃未被接取任务
     * @param $id
     * @return mixed
     */
    public function CancelStatus($id)
    {
        $sql = "update jd_task_jd_list  set `status`= 4   where taskid =$id   AND  `status`=0 ";
        return $r = $this->db->query($sql);
    }


}
